﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.SystemSetting
{
    public partial class FormBaseData : Waker.Base.FormBaseToolBar
    {
        public FormBaseData()
        {
            InitializeComponent();
        }

        private void FormBaseData_Load(object sender, EventArgs e)
        {
            FormRefresh();
        }
        public override void FormRefresh()
        {
            var dt = new BAL.BAL_SystemSetting().GetAllBaseData();
            this.gridControl1.DataSource = dt;
            this.gridView1.ExpandAllGroups();
        }
        protected override void FormAdd()
        {
            DataRow dr = null;
            if (gridView1.FocusedRowHandle >= 0)
            {
                dr = this.gridView1.GetFocusedDataRow();
            }
            var formAdd = new FormBaseDataEdit(dr, true);
            var res = formAdd.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        protected override void FormEdit()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            var form = new FormBaseDataEdit(dr, false);
            var res = form.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks > 1)
            {
                FormEdit();
            }
        }
        protected override void FormDelete()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null || dr["ID"] == DBNull.Value)
                return;
            if (UI.ShowWarningOKCancel(
                string.Format("确定要删除 {0}中的 [{1}]", dr["DataType"], dr["DataName"]),
                "基础数据删除提示(慎重操作)"
                ) != DialogResult.OK)
                return;
            var id = Convert.ToInt32(dr["ID"]);
            var isOK = new BAL.BAL_SystemSetting().DelteBaseData(id);
            if (isOK)
            {
                UI.ShowInfo("删除成功");
                FormRefresh();
            }
            else
            {
                UI.ShowError("删除失败");
            }
        }
    }
}
